package com.gist.cithub.service.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gist.cithub.service.entity.StarEntity;
import com.gist.cithub.service.service.StarService;
import com.gist.common.utils.PageUtils;
import com.gist.common.utils.R;



/**
 * 服务收藏
 *
 * @author lejin
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 13:29:16
 */
@RestController
@RequestMapping("service/star")
public class StarController {
    @Autowired
    private StarService starService;
    /**
     *判断是否收藏
     */
    @RequestMapping("/isStared/{serviceId}/{userId}")
    public R isStared(@PathVariable("serviceId") Integer serviceId,@PathVariable("userId") Integer userId){
        StarEntity star = starService.isStared(serviceId,userId);

        return R.ok().put("star", star!=null);
    }

    /**
     * 获取收藏数
     * @param serviceId
     * @return
     */
    @RequestMapping("/getStarNum/{serviceId}")
    public R getStarNum(@PathVariable("serviceId") Integer serviceId){
        int starNum = starService.getStarNum(serviceId);

        return R.ok().put("starNum", starNum);
    }

    /**
     * 获取用户收藏服务
     * @param
     * @return
     */
    @RequestMapping("/getStar/{userId}")
    public R getStar(@PathVariable("userId") Integer userId){
        List<StarEntity> starEntityList= starService.getStarByUserId(userId);
        return R.ok().put("list", starEntityList).put("size",starEntityList.size());
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = starService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		StarEntity star = starService.getById(id);

        return R.ok().put("star", star);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody StarEntity star){
		starService.save(star);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody StarEntity star){
		starService.updateById(star);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		starService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
